package com.cskaoyan.service.front;

import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.po.MarketRegion;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.mapper.MarketRegionMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @auther 10-何兴绕
 * @Date 2022年10月13日 23:14
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    MarketAddressMapper addressMapper;

    @Autowired
    MarketRegionMapper regionMapper;

    @Override
    public CommonData showAddress() {
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");

        Integer id = addressMapper.selectUserIdByUsername(username);

        MarketAddressExample exampleCount = new MarketAddressExample();
        MarketAddressExample.Criteria countCriteria = exampleCount.createCriteria();
        countCriteria.andUserIdEqualTo(id);
        countCriteria.andDeletedEqualTo(false);
        long count = addressMapper.countByExample(exampleCount);
        MarketAddressExample example = new MarketAddressExample();
        example.setOrderByClause("add_time desc");
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(example);
        CommonData<MarketAddress> commonData = new CommonData<>();

        for (MarketAddress address : marketAddresses) {
            MarketRegion provence = regionMapper.selectByPrimaryKey(Integer.parseInt(address.getProvince()));
            MarketRegion city = regionMapper.selectByPrimaryKey(Integer.parseInt(address.getCity()));
            MarketRegion county = regionMapper.selectByPrimaryKey(Integer.parseInt(address.getCounty()));
            address.setProvince(provence.getName());
            address.setCity(city.getName());
            address.setCounty(county.getName());
        }

        commonData.setTotal((int) count);
        commonData.setPages(1);
        commonData.setLimit(marketAddresses.size());
        commonData.setPage(1);
        commonData.setList(marketAddresses);
        return commonData;
    }

    @Override
    public Integer addAddress(MarketAddress marketAddress) {
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        Integer userId = addressMapper.selectUserIdByUsername(username);

        if (marketAddress.getIsDefault()) {
            addressMapper.updateIs_default(userId);
        }

        String provinceName = marketAddress.getProvince();
        MarketRegion provence = regionMapper.selectByProvenceNameAndPid(provinceName, 0);
        String cityName = marketAddress.getCity();
        MarketRegion city = regionMapper.selectByProvenceNameAndPid(cityName, provence.getId());
        String countyName = marketAddress.getCounty();
        MarketRegion county = regionMapper.selectByProvenceNameAndPid(countyName, city.getId());

        MarketAddress address;
        address = marketAddress;
        address.setProvince(provence.getId().toString());
        address.setCity(city.getId().toString());
        address.setCounty(county.getId().toString());
        address.setPostalCode(county.getCode().toString());
        address.setAreaCode(county.getCode().toString());
        address.setUserId(userId);
        address.setAddTime(new Date());
        address.setUpdateTime(new Date());

        Integer id = marketAddress.getId();
        if (id == 0) {
            addressMapper.insertSelective(address);
        } else {
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(marketAddress.getId());
            address.setUpdateTime(new Date());
            addressMapper.updateByExampleSelective(address, example);
        }
        return address.getId();
    }

    @Override
    public MarketAddress echoAddress(Integer id) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);

        String provinceId = marketAddress.getProvince();
        String cityId = marketAddress.getCity();
        String countyId = marketAddress.getCounty();
        MarketRegion provence = regionMapper.selectByPrimaryKey(Integer.parseInt(provinceId));
        MarketRegion city = regionMapper.selectByPrimaryKey(Integer.parseInt(cityId));
        MarketRegion county = regionMapper.selectByPrimaryKey(Integer.parseInt(countyId));

        marketAddress.setProvince(provence.getName());
        marketAddress.setCity(city.getName());
        marketAddress.setCounty(county.getName());
        return marketAddress;
    }

    @Override
    public int deleteAddress(MarketAddress marketAddress) {
        marketAddress.setDeleted(true);
        addressMapper.updateByPrimaryKeySelective(marketAddress);
        return 0;
    }
}
